CFLAGS += -fno-strict-aliasing
CFLAGS += $(INCLUDES) -I.
+# Needed for posix_fadvise64() in xc_linux.c
+CFLAGS-$(CONFIG_Linux) += -D_GNU_SOURCE
+
# Define this to make it possible to run valgrind on code linked with these
# libraries.
#CFLAGS += -DVALGRIND -O0 -ggdb3
}
}
+ if (length >= DUMP_INCREMENT*PAGE_SIZE) {
+ // Now dumping pages -- make sure we discard clean pages from
+ // the cache after each write
+ discard_file_cache(da->fd, 0 /* no flush */);
+ }
+
return 0;
}
sts = xc_domain_dumpcore_via_callback(
xc_handle, domid, &da, &local_file_dump);
+ /* flush and discard any remaining portion of the file from cache */
+ discard_file_cache(da.fd, 1/* flush first*/);
+
close(da.fd);
return sts;
return dorw(xce_handle, (char *)&port, sizeof(port), 1);
}
+/* Optionally flush file to disk and discard page cache */
+int discard_file_cache(int fd, int flush)
+{
+ off_t cur = 0;
+
+ if ( flush && (fsync(fd) < 0) )
+ {
+ PERROR("Failed to flush file: %s", strerror(errno));
+ return -errno;
+ }
+
+ /*
+ * Calculate last page boundary of amount written so far
+ * unless we are flushing in which case entire cache
+ * is discarded.
+ */
+ if ( !flush )
+ {
+ if ( (cur = lseek(fd, 0, SEEK_CUR)) == (off_t)-1 )
+ cur = 0;
+ cur &= ~(PAGE_SIZE-1);
+ }
+
+ /* Discard from the buffer cache. */
+ if ( posix_fadvise64(fd, 0, cur, POSIX_FADV_DONTNEED) < 0 )
+ {
+ PERROR("Failed to discard cache: %s", strerror(errno));
+ return -errno;
+ }
+
+ return 0;
+}
+
/*
* Local variables:
* mode: C
unsigned int console_evtchn, unsigned long *console_mfn)
{
DECLARE_DOMCTL;
- int rc = 1, i, n, pae_extended_cr3 = 0;
+ int rc = 1, i, n, m, pae_extended_cr3 = 0;
unsigned long mfn, pfn;
unsigned int prev_pc, this_pc;
int verify = 0;
*/
prev_pc = 0;
- n = 0;
+ n = m = 0;
while (1) {
int j, nr_mfns = 0;
munmap(region_base, j*PAGE_SIZE);
n+= j; /* crude stats */
+
+ /*
+ * Discard cache for portion of file read so far up to last
+ * page boundary every 16MB or so.
+ */
+ m += j;
+ if ( m > MAX_PAGECACHE_USAGE )
+ {
+ discard_file_cache(io_fd, 0 /* no flush */);
+ m = 0;
+ }
}
/*
free(p2m);
free(pfn_type);
+ /* discard cache for save file */
+ discard_file_cache(io_fd, 1 /*flush*/);
+
DPRINTF("Restore exit with rc=%d\n", rc);
return rc;
(new->tv_usec - old->tv_usec);
}
+static int noncached_write(int fd, int live, void *buffer, int len)
+{
+ static int write_count = 0;
+
+ int rc = write(fd,buffer,len);
+
+ if (!live) {
+ write_count += len;
+
+ if (write_count >= MAX_PAGECACHE_USAGE*PAGE_SIZE) {
+ int serrno = errno;
+
+ /* Time to discard cache - dont care if this fails */
+ discard_file_cache(fd, 0 /* no flush */);
+
+ write_count = 0;
+
+ errno = serrno;
+ }
+ }
+ return rc;
+}
#ifdef ADAPTIVE_SAVE
}
-static int ratewrite(int io_fd, void *buf, int n)
+static int ratewrite(int io_fd, int live, void *buf, int n)
{
static int budget = 0;
static int burst_time_us = -1;
long long delta;
if (START_MBIT_RATE == 0)
- return write(io_fd, buf, n);
+ return noncached_write(io_fd, live, buf, n);
budget -= n;
if (budget < 0) {
}
}
}
- return write(io_fd, buf, n);
+ return noncached_write(io_fd, live, buf, n);
}
#else /* ! ADAPTIVE SAVE */
#define RATE_IS_MAX() (0)
-#define ratewrite(_io_fd, _buf, _n) write((_io_fd), (_buf), (_n))
+#define ratewrite(_io_fd, _live, _buf, _n) noncached_write((_io_fd), (_live), (_buf), (_n))
#define initialize_mbit_rate()
#endif
if(race && !live)
goto out;
- if (ratewrite(io_fd, page, PAGE_SIZE) != PAGE_SIZE) {
+ if (ratewrite(io_fd, live, page, PAGE_SIZE) != PAGE_SIZE) {
ERROR("Error when writing to state file (4)"
" (errno %d)", errno);
goto out;
} else {
/* We have a normal page: just write it directly. */
- if (ratewrite(io_fd, spage, PAGE_SIZE) != PAGE_SIZE) {
+ if (ratewrite(io_fd, live, spage, PAGE_SIZE) != PAGE_SIZE) {
ERROR("Error when writing to state file (5)"
" (errno %d)", errno);
goto out;
DPRINTF("Warning - couldn't disable shadow mode");
}
}
+ else {
+ // flush last write and discard cache for file
+ discard_file_cache(io_fd, 1 /* flush */);
+ }
if (live_shinfo)
munmap(live_shinfo, PAGE_SIZE);
#define INFO 1
#define PROGRESS 0
+/*
+** Define max dirty page cache to permit during save/restore -- need to balance
+** keeping cache usage down with CPU impact of invalidating too often.
+** (Currently 16MB)
+*/
+#define MAX_PAGECACHE_USAGE (4*1024)
+
#if INFO
#define IPRINTF(_f, _a...) printf(_f , ## _a)
#else
void bitmap_64_to_byte(uint8_t *bp, const uint64_t *lp, int nbits);
void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
+/* Optionally flush file to disk and discard page cache */
+int discard_file_cache(int fd, int flush);
+
#endif /* __XC_PRIVATE_H__ */
{
return dorw(xce_handle, (char *)&port, sizeof(port), 1);
}
+
+/* Optionally flush file to disk and discard page cache */
+int discard_file_cache(int fd, int flush)
+{
+ // TODO: Implement for Solaris!
+ return 0;
+}